﻿using LaoLuo;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Linq;

namespace LaoLuo.Common.Test
{
    
    
    /// <summary>
    ///这是 IEnumerableExtensionsTest 的测试类，旨在
    ///包含所有 IEnumerableExtensionsTest 单元测试
    ///</summary>
    [TestClass()]
    public class IEnumerableExtensionsTest
    {


        private TestContext testContextInstance;

        /// <summary>
        ///获取或设置测试上下文，上下文提供
        ///有关当前测试运行及其功能的信息。
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region 附加测试特性
        // 
        //编写测试时，还可使用以下特性:
        //
        //使用 ClassInitialize 在运行类中的第一个测试前先运行代码
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //使用 ClassCleanup 在运行完类中的所有测试后再运行代码
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //使用 TestInitialize 在运行每个测试前先运行代码
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //使用 TestCleanup 在运行完每个测试后运行代码
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion

        [TestMethod()]
        public void AllSameReferenceTest()
        {
            var value = new Class1();
            var test1 = new List<Class1>();
            for (int i = 0; i < 100; i++)
            {
                test1.Add(value);
            }
            Assert.AreEqual(100, test1.Count);
            Assert.IsFalse(new List<Class1> { new Class1(), new Class1() }.ShareSameReference());
            Assert.IsTrue(test1.ShareSameReference());
        }

        /// <summary>
        ///GetJoint 的测试
        ///</summary>
        public void GetJointTestHelper<T>(IEnumerable<T> values, string separator, string expected)
        {
            Assert.AreEqual(expected, values.GetJoint<T>(separator));
        }

        [TestMethod()]
        public void GetJointTest()
        {
            GetJointTestHelper<int>(new List<int> { 1, 2, 3, 4, 5, 9, 8 }, ",", "1,2,3,4,5,9,8");
            GetJointTestHelper<string>(new List<string> { "a","b","c" }, "!", "a!b!c");
        }

        /// <summary>
        ///ShareSameReference 的测试
        ///</summary>
        public void ShareSameReferenceTestHelper<T>(IEnumerable<T> values, bool expected)
            where T : class
        {
            Assert.AreEqual(expected, values.ShareSameReference());
        }

        [TestMethod()]
        public void ShareSameReferenceTest()
        {
            var obj = new Class1();
            ShareSameReferenceTestHelper<Class1>(new List<Class1> { new Class1(), new Class1() }, false);
            ShareSameReferenceTestHelper<Class1>(new List<Class1> { obj, obj }, true);

            List<Class1> l = null;
            try
            {
                l.ShareSameReference();
                Assert.IsTrue(false);
            }
            catch (Exception ex)
            {
                Assert.IsTrue(ex is ArgumentException);
            }

            l = new List<Class1>();
            try
            {
                l.ShareSameReference();
                Assert.IsTrue(false);
            }
            catch (Exception ex)
            {
                Assert.IsTrue(ex is ArgumentException);
            }
        }

        [TestMethod()]
        public void DistinctTest()
        {
            var c1 = new Class1 { Name  = "chester", Value = 100};
            var c2 = new Class1 { Name = "chester", Value = 200 };
            var cList = new List<Class1> {c1, c2};
            Assert.AreEqual(1, cList.Distinct(i => i.Name).Count());

            c2.Name = "c2";
            var cList2 = new List<Class1> { c1, c2 };
            Assert.AreEqual(2, cList2.Distinct(i => i.Name).Count());
        }
    }
}
